package com.jlboot.model.po.sys;

import com.jfinal.core.*;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jlboot.core.annotation.JlActivity;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.util.LoginUtil;
import com.jlboot.core.util.StringUtil;
import com.jlboot.core.base.Models;
import com.jfinal.plugin.activerecord.Model;
import com.jlboot.app.admin.interceptor.AuthActivity;
import com.jlboot.core.cache.SysRoleCache;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * table: sys_activity
 * author: wuyang
 * Date: 2022-08-01 11:41:34
 */
public class SysActivity extends Model<SysActivity> {

    public static final SysActivity dao = new SysActivity().dao();
    public static final String table="sys_activity";
    public Models mod = Models.n(this);

    public SysActivity setCommon(){
        Date now=new Date();
        this.set("id",SysIdGenerator.dao.getNextId(table));
        this.set("del_tag",Consts.COMMON_NONE_DEL);
        this.set("create_time",now);
        this.set("update_time",now);
        this.set("create_by", LoginUtil.getUserId());
        this.set("update_by",LoginUtil.getUserId());
        return this;
    }

    public static String getNextId(){
        return SysIdGenerator.dao.getNextId(table);
    }

    public AuthActivity saveSysActivity(AuthActivity am) {
        am.setAct_role(StringUtil.replace(am.getAct_role(), ",", "|"));
        SysActivity act = SysActivity.dao.findFirst("select * from sys_activity where act_path=?", am.getAct_path());
        if (act == null) {
            String act_role_name = SysRoleCache.transRoleName(am.getAct_role());
            new SysActivity()
                    .setCommon()
                    .set("id",SysActivity.getNextId())
                    .set("act_name", am.getAct_name())
                    .set("act_path", am.getAct_path())
                    .set("act_role", am.getAct_role())
                    .set("act_role_name", act_role_name)
                    .set("is_log", am.isIs_log())
                    .save();
        }
        return am;
    }

    public void updateActivity(){

        Date now=new Date();
        List<SysActivity> sysActivityList=new ArrayList<>();

        for (String actionKey : JFinal.me().getAllActionKeys()) {
            Action action=JFinal.me().getAction(actionKey,null);
            Method method=action.getMethod();
            String pathId=action.getControllerClass().getName()+"."+method.getName();
            JlActivity jlActivity = method.getAnnotation(JlActivity.class);
            if (jlActivity != null) {
                String code = jlActivity.code();
                String name = jlActivity.name();
                boolean log = jlActivity.log();
                if (StrKit.isBlank(code)) {
                    code = pathId;
                }

                SysActivity sysActivity = new SysActivity()
                        .setCommon()
                        .set("create_time", now)
                        .set("update_time", now)
                        .set("create_by", Consts.SYSTEM_USER_ID_STRING)
                        .set("update_by", Consts.SYSTEM_USER_ID_STRING)
                        .set("path_id", pathId)
                        .set("code", code)
                        .set("act_name", name)
                        .set("act_path", actionKey)
                        .set("act_role", "")
                        .set("act_role_name", "")
                        .set("is_log", log);
                sysActivityList.add(sysActivity);

            }

        }

        List<SysActivity> addList=new ArrayList<>();
        List<SysActivity> updateList=new ArrayList<>();
        List<SysActivity> allList=SysActivity.dao.findAll();
        Map<String,SysActivity> sysActivityMap=allList.stream().collect(Collectors.toMap(t->t.getStr("path_id"),t->t));
        for (SysActivity sysActivity : sysActivityList) {
            SysActivity sysActivityHis=sysActivityMap.get(sysActivity.getStr("path_id"));
            if(sysActivityHis!=null){
                sysActivityHis.set("act_name",sysActivity.get("act_name"));
                sysActivityHis.set("act_path",sysActivity.get("act_path"));
                sysActivityHis.set("update_time",sysActivity.get("update_time"));
                sysActivityHis.set("update_by",sysActivity.get("update_by"));
                updateList.add(sysActivityHis);
            }else{
                addList.add(sysActivity);
            }
        }

        Db.batchSave(addList,1000);
        Db.batchUpdate(updateList,1000);

    }


    
}






